﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Research.TouchMouseSensor;

namespace IntelliTouchMix
{
    public partial class testForm : Form
    {
        BlobController bc;
        public testForm()
        {
            TouchMouseSensorEventManager.Handler += TouchMouseSensorHandler;
            bc = new BlobController(16, 130, this);
            InitializeComponent();
            //bc.OneFingerDownHandler += new Gesture(bc_FingerDownHandler);
            //bc.OneFingerUpHandler += new Gesture(bc_FingerUpHandler);
            bc.BigFingerDownHandler += new Gesture(bc_BigFingerDownHandler);
            bc.BigFingerDownRealTimeHandler += new Gesture(bc_BigFingerDownRealTimeHandler);
            bc.BigFingerLeftHandler += new Gesture(bc_BigFingerLeftHandler);
            bc.BigFingerLeftRealTimeHandler += new Gesture(bc_BigFingerLeftRealTimeHandler);
            bc.BigFingerRightHandler += new Gesture(bc_BigFingerRightHandler);
            bc.BigFingerRightRealTimeHandler += new Gesture(bc_BigFingerRightRealTimeHandler);
            bc.BigFingerUpHandler += new Gesture(bc_BigFingerUpHandler);
            bc.BigFingerUpRealTimeHandler += new Gesture(bc_BigFingerUpRealTimeHandler);
            bc.BigVerticalDownLeftHandler += new Gesture(bc_BigVerticalDownLeftHandler);
            bc.BigVerticalDownLeftRealTimeHandler += new Gesture(bc_BigVerticalDownLeftRealTimeHandler);
            bc.BigVerticalDownRightHandler += new Gesture(bc_BigVerticalDownRightHandler);
            bc.BigVerticalDownRightRealTimeHandler += new Gesture(bc_BigVerticalDownRightRealTimeHandler);
            bc.BigVerticalUpLeftHandler += new Gesture(bc_BigVerticalUpLeftHandler);
            bc.BigVerticalUpLeftRealTimeHandler += new Gesture(bc_BigVerticalUpLeftRealTimeHandler);
            bc.BigVerticalUpRightHandler += new Gesture(bc_BigVerticalUpRightHandler);
            bc.BigVerticalUpRightRealTimeHandler += new Gesture(bc_BigVerticalUpRightRealTimeHandler);
            bc.CInvertedNegativeHandler += new Gesture(bc_CInvertedNegativeHandler);
            bc.CInvertedPositiveHandler += new Gesture(bc_CInvertedPositiveHandler);
            bc.CircleNegativeHandler += new Gesture(bc_CircleNegativeHandler);
            bc.CircleNegativeRealTimeHandler += new Gesture(bc_CircleNegativeRealTimeHandler);
            bc.CirclePositiveHandler += new Gesture(bc_CirclePositiveHandler);
            bc.CirclePositiveRealTimeHandler += new Gesture(bc_CirclePositiveRealTimeHandler);
            bc.CNegativeHandler += new Gesture(bc_CNegativeHandler);
            bc.CPositiveHandler += new Gesture(bc_CPositiveHandler);
            // bc.FingerDownHandler+=new Gesture(bc_FingerDownHandler);
            bc.GestureHandler += new Gesture(bc_GestureHandler);
            bc.MaterialClothesHandler += new Gesture(bc_MaterialClothesHandler);
            bc.MaterialCoverHandler += new Gesture(bc_MaterialCoverHandler);
            bc.MaterialHairHandler += new Gesture(bc_MaterialHairHandler);
            bc.MaterialHandler += new Gesture(bc_MaterialHandler);
            bc.MaterialPaperHandler += new Gesture(bc_MaterialPaperHandler);
            bc.MaterialSkinHandler += new Gesture(bc_MaterialSkinHandler);
            bc.OneFingerDownHandler += new Gesture(bc_OneFingerDownHandler);
            bc.OneFingerDownRealTimeHandler += new Gesture(bc_OneFingerDownRealTimeHandler);
            bc.OneFingerLeftHandler += new Gesture(bc_OneFingerLeftHandler);
            bc.OneFingerLeftRealTimeHandler += new Gesture(bc_OneFingerLeftRealTimeHandler);
            bc.OneFingerRightHandler += new Gesture(bc_OneFingerRightHandler);
            bc.OneFingerRightRealTimeHandler += new Gesture(bc_OneFingerRightRealTimeHandler);
            bc.OneFingerUpHandler += new Gesture(bc_OneFingerUpHandler);
            bc.ThreeFingersDownHandler += new Gesture(bc_ThreeFingersDownHandler);
            bc.ThreeFingersDownRealTimeHandler += new Gesture(bc_ThreeFingersDownRealTimeHandler);
            bc.ThreeFingersUpHandler += new Gesture(bc_ThreeFingersUpHandler);
            bc.ThreeFingersUpRealTimeHandler += new Gesture(bc_ThreeFingersUpRealTimeHandler);
            bc.TwoFingersDownHandler+=new Gesture(bc_TwoFingersDownHandler);
            bc.TwoFingersDownRealTimeHandler += new Gesture(bc_TwoFingersDownRealTimeHandler);
            bc.TwoFingersUpHandler += new Gesture(bc_TwoFingersUpHandler);
            bc.TwoFingersUpRealTimeHandler += new Gesture(bc_TwoFingersUpRealTimeHandler);
            bc.UInvertedNegativeHandler += new Gesture(bc_UInvertedNegativeHandler);
            bc.UInvertedPositiveHandler += new Gesture(bc_UInvertedPositiveHandler);
            bc.UNegativeHandler += new Gesture(bc_UNegativeHandler);
            bc.UPositiveHandler += new Gesture(bc_UPositiveHandler);
            bc.VerticalDownLeftHandler += new Gesture(bc_VerticalDownLeftHandler);
            bc.VerticalDownLeftRealTimeHandler += new Gesture(bc_VerticalDownLeftRealTimeHandler);
            bc.VerticalDownRightHandler += new Gesture(bc_VerticalDownRightHandler);
            bc.VerticalDownRightRealTimeHandler += new Gesture(bc_VerticalDownRightRealTimeHandler);
            bc.VerticalUpLeftHandler += new Gesture(bc_VerticalUpLeftHandler);
            bc.VerticalUpLeftRealTimeHandler += new Gesture(bc_VerticalUpLeftRealTimeHandler);
            bc.VerticalUpRightHandler += new Gesture(bc_VerticalUpRightHandler);
            bc.VerticalUpRightRealTimeHandler += new Gesture(bc_VerticalUpRightRealTimeHandler);
            bc.TwoFingersLeftHandler += new Gesture(bc_TwoFingersLeftHandler);
            bc.TwoFingersRightHandler += new Gesture(bc_TwoFingersRightHandler);
            bc.ThreeFingersLeftHandler += new Gesture(bc_ThreeFingersLeftHandler);
            bc.ThreeFingersRightHandler += new Gesture(bc_ThreeFingersRightHandler);

            bc.LDownLeft += new Gesture(bc_LDownLeft);
            bc.LDownRight += new Gesture(bc_LDownRight);
            bc.LLeftDown += new Gesture(bc_LLeftDown);
            bc.LLeftUp += new Gesture(bc_LLeftUp);
            bc.LRightDown += new Gesture(bc_LRightDown);
            bc.LRightUp += new Gesture(bc_LRightUp);
            bc.LUpLeft += new Gesture(bc_LUpLeft);
            bc.LUpRight += new Gesture(bc_LUpRight);
          

           // bc.OneFingerUpRealTimeHandler += hallo;
           // bc.OneFingerDownRealTimeHandler += hallo;
            
           // Cursor.Dispose();

        }

        void bc_LUpRight(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("L Up Right" + e);
        }

        void bc_LUpLeft(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("L Up Left" + e);
        }

        void bc_LRightUp(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("L Right Up" + e);
        }

        void bc_LRightDown(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("L Right Down" + e);
        }

        void bc_LLeftUp(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("L Left Up " + e);
        }

        void bc_LLeftDown(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("L Left Down" + e);
        }

        void bc_LDownRight(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("L Down Right" + e);
        }

        void bc_LDownLeft(object sender, TouchMouseEventArgs e)
        {
            
            MessageBox.Show("L Down Left" + e);
        }

        void bc_ThreeFingersRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("3 dedos derecha" + e);
        }

        void bc_ThreeFingersLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("3 dedos izquierda" + e);
        }

        void bc_TwoFingersRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("2 dedos derecha" + e);
        }

        void bc_TwoFingersLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("2 dedos izquierda" + e);
        }

        void bc_VerticalUpRightRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Real Time Vertica up Left" + e);
        }

        void bc_VerticalUpRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Vertica up Rigth" + e);
        }

        void bc_VerticalUpLeftRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("VerticalUpLeftRealTime" + e);
        }

        void bc_VerticalUpLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("VerticalUpLeft" + e);
        }

        void bc_VerticalDownRightRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("VerticalDownRightRealTime" + e);
        }

        void bc_VerticalDownRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("VerticalDownRightHandler" + e);
        }

        void bc_VerticalDownLeftRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("VerticalDownLeftRealTimeHandler" + e);
        }

        void bc_VerticalDownLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("VerticalDownLeftHandler" + e);
        }

        void bc_UPositiveHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("UPositiveHandler" + e);
        }

        void bc_UNegativeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("UNegativeHandler" + e);
        }

        void bc_UInvertedPositiveHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("UInvertedPositiveHandler" + e);
        }

        void bc_UInvertedNegativeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("UInvertedNegativeHandler" + e);
        }

        void bc_TwoFingersUpRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("TwoFingersUpRealTimeHandler" + e);
        }

        void bc_TwoFingersUpHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("TwoFingersUpHandler" + e);
        }

        void bc_TwoFingersDownRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("TwoFingersDownRealTimeHandler" + e);
        }

        void bc_ThreeFingersUpRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("ThreeFingersUpRealTimeHandler" + e);
        }

        void bc_ThreeFingersUpHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("ThreeFingersUpHandler" + e);
        }

        void bc_ThreeFingersDownRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("ThreeFingersDownRealTimeHandler" + e);
        }

        void bc_ThreeFingersDownHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("3 fingers down" + e);
        }

        void bc_OneFingerUpHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("one finger up" + e);
        }

        void bc_OneFingerRightRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Real Time one finger right" + e);
        }

        void bc_OneFingerRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("one finger rigth" + e);
        }

        void bc_OneFingerLeftRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("One Figer Left Real Time" + e);
        }

        void bc_OneFingerLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("one finger left" + e);
        }

        void bc_OneFingerDownRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("one finger down Real time" + e);
        }

        void bc_OneFingerDownHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("dedo abajo " + e);
        }

        void bc_MaterialSkinHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Material change to Skin" + e);
        }

        void bc_MaterialPaperHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Material change to paper" + e);
        }

        void bc_MaterialHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Material change" + e);
        }

        void bc_MaterialHairHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Material change to hair" + e);
        }

        void bc_MaterialCoverHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("material change to cover" + e);
        }

        void bc_MaterialClothesHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("material change to clothes" + e);
        }

        void bc_GestureHandler(object sender, TouchMouseEventArgs e)
        {
           // MessageBox.Show("2 dedos abajo " + e);
        }

        void bc_CPositiveHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("C positive" + e);
        }

        void bc_CNegativeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("C negative " + e);
        }

        void bc_CirclePositiveRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("circulo positivo real time" + e);
        }

        void bc_CirclePositiveHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("circulo positivo " + e);
        }

        void bc_CircleNegativeRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("circulo negativo ral time " + e);
        }

        void bc_CircleNegativeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("circulo negativo " + e);
        }

        void bc_CInvertedPositiveHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("C invertida positva" + e);
        }

        void bc_CInvertedNegativeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("C invertida Negativa" + e);
        }

        void bc_BigVerticalUpRightRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big vertical up left" + e);
        }

        void bc_BigVerticalUpRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big V Up Right " + e);
        }

        void bc_BigVerticalUpLeftRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big V Up Left Real Time" + e);
        }

        void bc_BigVerticalUpLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big V Up Left " + e);
        }

        void bc_BigVerticalDownRightRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big V Down Right Real TIme" + e);
        }

        void bc_BigVerticalDownRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big Vertical DOwn Right" + e);
        }

        void bc_BigVerticalDownLeftRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big V Down Left Real" + e);
        }

        void bc_BigVerticalDownLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big V Dow Left Handler" + e);
        }

        void bc_BigFingerUpRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("BIg Up Real Time " + e);
        }

        void bc_BigFingerUpHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("Big Up" + e);
        }

        void bc_BigFingerRightRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("BigFingerRightRealTime" + e);
        }

        void bc_BigFingerRightHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("BigFingerRight " + e);
        }

        void bc_BigFingerLeftRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("FingerLeftRealTime " + e);
        }

        void bc_BigFingerLeftHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("BigFingerLeft " + e);
        }

        void bc_BigFingerDownRealTimeHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("BigFingerDownRealTime " + e);
        }

        void bc_BigFingerDownHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("bc_BigFingerDownHandler " + e);
        }

        void bc_TwoFingersDownHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("2 dedos abajo "+e);
        }
        /*
        void bc_FingerUpHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("dedo Ariba "+e);
        }

        void bc_FingerDownHandler(object sender, TouchMouseEventArgs e)
        {
            MessageBox.Show("dedo abajo "+e);
        }
         * */
        int i = 0;
        public void hallo(object sender, TouchMouseEventArgs e){
           //MessageBox.Show("Hallo");
            if(e==null)
                MessageBox.Show("error");
            if (e.senseType==SenseType.NEGATIVE)
                vScrollBar1.Value += (int)(e.velocity*500.0f);
            else if (e.senseType == SenseType.POSITIVE)
                vScrollBar1.Value -= (int)(e.velocity * 500.0f);
            Console.WriteLine(i+" "+e.senseType+" S "+e.velocity);
            i++; if (i > 100) i = 0;
            // addText("\n"+e.senseType);
        }
        void tany(object sender, TouchMouseEventArgs e)
        {
            // MessageBox.Show("HAlllo " + e.ToString());
            addText("3");
        }
        void TouchMouseSensorHandler(object sender, TouchMouseSensorEventArgs e)
        {
            // We're in a thread belonging to the mouse, not the user interface 
            // thread. Need to dispatch to the user interface thread.
            //Dispatcher.Invoke((Action<TouchMouseSensorEventArgs>)SetSource, e);
            //Dispatcher.Invoke((Action<TouchMouseSensorEventArgs>)SetSource, e);
            try
            {
                // SetSource(e);
                Invoke((Action<TouchMouseSensorEventArgs>)SetSource, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("jojo - "+ex.StackTrace);
            }
        }
        public void setText(String s)
        {
            TestLabel.Text = s;
        }
        public void addText(String s)
        {
            TestLabel.Text += "\n" + s;
        }
        void SetSource(TouchMouseSensorEventArgs e)
        {
            int w = e.Status.m_dwImageWidth;
            int h = e.Status.m_dwImageHeight;
            
            Cursor.Position = new Point(Location.X+70,Location.Y+70);
            //Cursor.Position.Offset(50,50);
            // Dispatcher.Invoke((Action)(Hi));
            bc.actualize(e);

            // out_text.Text = "jojojo\nte quioer";
            // MessageBox.Show("w " + w + " h " + h);
            /*
             BitmapSource source =
                 BitmapSource.Create(e.Status.m_dwImageWidth, e.Status.m_dwImageHeight,
                 96, 96,
                 PixelFormats.Gray8, null, e.Image, e.Status.m_dwImageWidth);
            
             // Blobs[] blobs = bControler.find(e.Image, w, h);

             // Show bitmap in user interface.
             SensorImage.Source = source;
             // Print Blob data
             output.Content = "";
             for (int i = 0; i < bControler.iblob; ++i)
             {
                 output.Content += i + " " + blobs[i].ToString() + "\n";
             }*/
        }

        private void testForm_Load(object sender, EventArgs e)
        {

        }

        private void mueve(object sender, EventArgs e)
        {
            Cursor.Position = new Point(Location.X + 70, Location.Y + 70);
            Cursor.Hide();
        }


       

    }
}
